package com.zsh.adapters;

import java.util.List;

import com.android.volley.toolbox.NetworkImageView;
import com.zsh.entities.MerchandiseItem;
import com.zsh.prototype.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainListAdapter extends ArrayAdapter<MerchandiseItem> {

	public static final String TAG = "ZSH";
	private List<MerchandiseItem> products;
	private Context context;

	public MainListAdapter(Context context, List<MerchandiseItem> products) {
		super(context, R.layout.mainlist_item, products);
		this.products = products;
		this.context = context;
	}

	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ProductViewHolder holder = new ProductViewHolder();
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.mainlist_item, parent,
					false);
			ImageButton starImageButton = (ImageButton) convertView
					.findViewById(R.id.star);
			TextView cateTextView = (TextView) convertView
					.findViewById(R.id.category);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.title);
			
			TextView useridView = (TextView) convertView
					.findViewById(R.id.userid);
			TextView hotnessView = (TextView) convertView
					.findViewById(R.id.hotness);
			TextView commentsTextView = (TextView) convertView
					.findViewById(R.id.comment);

			NetworkImageView thumbImageView = (NetworkImageView) convertView
					.findViewById(R.id.thumbImage);
			
			touchDelegate(starImageButton, (View)starImageButton.getParent(), 30);
			holder.starImageButton = starImageButton;
			holder.cateTextView = cateTextView;
			holder.useridTextView = useridView;
			holder.hotTextView = hotnessView;
			holder.commentsTextView = commentsTextView;
			holder.titleTextView = titleTextView;
			holder.thumbImageView = thumbImageView;
			
			if (holder.thumbImageView.getVisibility() == View.GONE) {
				((RelativeLayout)commentsTextView.getParent()).setPadding(0, 0, 20, 0);
			}

			convertView.setTag(holder);
		} else {
			holder = (ProductViewHolder) convertView.getTag();
		}

		MerchandiseItem product = products.get(position);

		holder.titleTextView.setText(product.getSnippet());
		holder.cateTextView.setText(String.valueOf(product.getCategory()));
		holder.useridTextView.setText(product.getFindUserName());
		holder.hotTextView.setText(String.valueOf(product.getGoodCount()+product.getBadCount()));
		holder.commentsTextView.setText(String.valueOf(product.getCommentsCount()));
		holder.starImageButton.setOnClickListener(new OnClick(holder, product));

		
/*		if (holder.thumbImageView != null) {  
			holder.thumbImageView.setImageUrl(product.getPicPaths().get(0),VolleyHelper.getImageLoader());
		}*/
		

		return convertView;
	}
	
	private static class ProductViewHolder {
		public ImageButton starImageButton;
		public TextView cateTextView;
		public TextView titleTextView;
		public TextView useridTextView;
		public TextView hotTextView;
		public TextView commentsTextView;
		public NetworkImageView thumbImageView;
	}

	public class OnClick implements OnClickListener {
		private ProductViewHolder holder;
		private MerchandiseItem product;

		public OnClick(ProductViewHolder holder, MerchandiseItem product) {
			this.holder = holder;
			this.product = product;
		}

		@Override
		public void onClick(View v) {
			if (holder.starImageButton.isSelected()) {
				holder.starImageButton.setSelected(false);
				Log.d(TAG, product.getSnippet() + "is selected");
			} else {
				holder.starImageButton.setSelected(true);
				Log.d(TAG, product.getSnippet() + "is unselected");
			}
		}
	}

	private void touchDelegate(final View delegate, final View parent,
			final int sizeInDp) {
		if (parent != null && delegate != null) {
			parent.post(new Runnable() {
				@Override
				public void run() {
					Rect delegateArea = new Rect();
					delegate.getHitRect(delegateArea);
					Resources res = delegate.getContext().getResources();
					int sizeInPixels = (int) TypedValue.applyDimension(
							TypedValue.COMPLEX_UNIT_DIP, sizeInDp,
							res.getDisplayMetrics());
					delegateArea.inset(-sizeInPixels, -sizeInPixels);
					TouchDelegate expandedArea = new TouchDelegate(
							delegateArea, delegate);
					parent.setTouchDelegate(expandedArea);
				}
			});
		}
	}

}
